using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using Lidgren.Network;

namespace SolarWinds
{
    public enum MessageTypes
    {
        PLAYER_JOINED = 0,
        PLAYER_LEFT = 1,
        PLAYER_MOVED = 2
    }

    public abstract class NetworkMessage
    {
        private MessageTypes mType;
        public MessageTypes Type
        {
            get { return mType; }
        }

        public NetworkMessage(MessageTypes type)
        {
            mType = type;
        }
        
        
        /// <summary>
        /// Writes message to the buffer
        /// </summary>
        /// <param name="buffer"></param>
        public virtual void ToBuffer(NetBuffer buffer)
        {
            buffer.Write((int)mType);
        }

        /// <summary>
        /// Read message from the buffer
        /// </summary>
        /// <param name="buffer"></param>
        public abstract void FromBuffer(NetBuffer buffer);
    }

    public class PlayerJoinedMessage : NetworkMessage
    {
        public String mPlayerName;

        public PlayerJoinedMessage()
            : base(MessageTypes.PLAYER_JOINED)
        {
        }

        public PlayerJoinedMessage(String playerName)
            : this()
        {
            mPlayerName = playerName;
        }

        public override void ToBuffer(NetBuffer buffer)
        {
            base.ToBuffer(buffer);

            buffer.Write(mPlayerName);
        }

        public override void FromBuffer(NetBuffer buffer)
        {
            mPlayerName = buffer.ReadString();
        }
    }

    public class PlayerLeftMessage : NetworkMessage
    {
        public String mPlayerName;

        public PlayerLeftMessage()
            : base(MessageTypes.PLAYER_LEFT)
        {
        }

        public PlayerLeftMessage(String playerName)
            : this()
        {
            mPlayerName = playerName;
        }

        public override void ToBuffer(NetBuffer buffer)
        {
            base.ToBuffer(buffer);

            buffer.Write(mPlayerName);
        }

        public override void FromBuffer(NetBuffer buffer)
        {
            mPlayerName = buffer.ReadString();
        }
    }

    /*public class PlayerMovedMessage : NetworkMessage
    {
        public String mPlayerName;
        // TODO public Position2D mPosition;

        public PlayerMovedMessage()
            : base(MessageTypes.PLAYER_MOVED)
        {
        }

        public PlayerMovedMessage(String playerName, Position2D pos) 
            : this()
        {
            mPlayerName = playerName;
            mPosition = pos;
        }

        public override void ToBuffer(NetBuffer buffer)
        {
            base.ToBuffer(buffer);

            buffer.Write(mPlayerName);
            buffer.Write(mPosition.X);
            buffer.Write(mPosition.X);
        }

        public override void FromBuffer(NetBuffer buffer)
        {
            mPlayerName = buffer.ReadString();
            // TODO mPosition = Position2D.From(buffer.ReadInt32(), buffer.ReadInt32());
        }
    }*/
}
